/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import fileIO from '@ohos.fileio';
import featureAbility from '@ohos.ability.featureAbility'
import okhttp from 'okhttp_ohos';
import Log from '../../log/log';
import router from '@system.router';
import defaultConfigJSON from './defaultConfig.json';

export default {
    data: {
        status: "",
        content: "",
        echoServer: "http://121.244.27.58:12269/echoserver",
        redirectServer: "http://121.244.27.58:12269/redirect/todifferentdomain",
        redirectServer2: "http://121.244.27.58:12269/redirect/tosamedomain",
        redirectServer3: "http://192.168.0.105:8080/test/dotolist/",
        fileServer: "http://121.244.27.58:12269/files/uploadfile",
        fileName: "/test.txt",
        client: new okhttp.OkHttpClient.Builder().setConnectTimeout(10000).build(),
        ws:""
    },

    onInit() {
        this.newCallRequest();
    },

    showSampleView() {
        router.push({
            uri: 'pages/samples/sample'
        })
    },

    showOkio() {
        router.push({
            uri: 'pages/okio_index/okio_index'
        })
    },

    onSuccess:function(result){
        this.content = result;
    },
    onFailure:function(error){
        this.content = error;
    },

    onDownloadTaskStart: function (downloadTask) {
        downloadTask.on('complete', () => {
            this.content = "Download Task Completed";
        })
    },

    onComplete: function (result) {
        if (result.response) {
            this.status = result.response.responseCode;
        }
        if (result.response.result)
            this.content = result.response.result;
        else
            this.content = JSON.stringify(result.response);

        Log.showInfo("onComplete -> Status : " + this.status);
        Log.showInfo("onComplete -> Content : " + JSON.stringify(this.content));
    },

    onError: function (error) {
         this.status = "";
        this.content = error.data;
        Log.showInfo("onError -> Error : " + this.content);
    },

    onRequest: function (req) {
        console.info("onRequest");
    },

    onResponse: function (res) {
        console.info("onResponse");
    },

    onRequest2: function (req) {
        console.info("onRequest2");
        req.url = req.url + "&foo3=bar3";
        return req;
    },

    onResponse2: function (res) {
        console.info("onResponse2");
    },

    onRequest3: function (req) {
        console.info("onRequest3");

        req.url = req.url + "&foo4=bar4";
        return req;
    },

    onResponse3: function (res) {
        console.info("onResponse3");
        resp.responseCode = 404;
    },

    webSocketConnect(){
        this.ws = new okhttp.WebSocket();
        this.ws.url("ws://192.168.1.6:5000/").connect(this.onSuccess,this.onFailure);

    },
    send(){
        this.ws.sendMessage("Hello",this.onSuccess,this.onFailure)
    },


    Disconnect(){
        this.ws.webSocketDisconnect(this.onSuccess,this.onFailure)
    },

    sendGetRequest() {
        let request = new okhttp.Request.Builder()
            .GET("https://postman-echo.com/get?foo1=bar1&foo2=bar2")
            .addHeader("Content-Type", "application/json")
            .params("testKey1", "testValue1")
            .params("testKey2", "testValue2")
            .build();

        this.client.newCall(request).enqueue(this.onComplete, this.onError);
    },

    sendPostRequest() {
        var request = new okhttp.Request.Builder()
            .url("https://postman-echo.com/post")
            .POST(okhttp.RequestBody.create("test123"))
            .addHeader("Content-Type", "application/json")
            .build();
        this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },

    sendPostRequest2() {
        var request = new okhttp.Request.Builder()
                .url("https://postman-echo.com/post")
                .POST(okhttp.RequestBody.create({
                    a: 'a1', b: 'b1'
                }, new okhttp.Mime.Builder().contentType('application/json', 'charset', 'utf8').build().getMime()))
                .build();
        this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },

    sendPostRequest3() {
        let formEncoder = new okhttp.FormEncoder.Builder()
                .add('key1', 'value1')
                .add('key2', 'value2')
                .build();
        let fe_body = formEncoder.createRequestBody();
        var request = new okhttp.Request.Builder()
                .url("https://postman-echo.com/post")
                .POST(fe_body)
                .build();
        this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },

    sendRedirectRequest() {
        var localClient = new okhttp.OkHttpClient.Builder()
            .setConnectTimeout(10000)
            .build();

        var request = new okhttp.Request.Builder()
            .GET(this.redirectServer)
            .addHeader("Content-Type", "application/json")
            .followRedirects(true)
            .retryOnConnectionFailure(true)
            .redirectMaxLimit(20)
            .retryMaxLimit(20)
            .build();

        localClient.newCall(request)
            .execute()
            .then(this.onComplete)
            .catch(this.onError);
    },

    async uploadRequest() {
        let appInternalDir;
        // @ts-ignore
        var fAbility = featureAbility.getContext().getCacheDir();
        await fAbility.then(function (result) {
            appInternalDir = result;
        });
        let fpath = appInternalDir + this.fileName;
        let fd = fileIO.openSync(fpath, 0o102, 0o666);
        // @ts-ignore
        fileIO.writeSync(fd, "text.txt file is uploaded", function (err, bytesWritten) {
        });
        fileIO.closeSync(fd);
        fpath = fpath.replace(appInternalDir, "internal://cache");
        var fileUploadBuilder = new okhttp.FileUpload.Builder()
                .addFile(fpath)
                .addData("name2", "value2")
                .build();
        var fileObject = fileUploadBuilder.getFile();
        var dataObject = fileUploadBuilder.getData();
        let request = new okhttp.Request.Builder()
                .url(this.fileServer)
                .addFileParams(fileObject, dataObject)
                .build();
        this.client.newCall(request)
                .execute()
                .then(this.onComplete)
                .catch(this.onError);
    },

    putRequest() {
        var request = new okhttp.Request.Builder()
                .url("https://postman-echo.com/put")
                .PUT(okhttp.RequestBody.create({
                    a: 'a1', b: 'b1'
                }, new okhttp.Mime.Builder().contentType('application/json', 'charset', 'utf8').build()))
                .build();
        this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },

    deleteRequest() {
        var request = new okhttp.Request.Builder()
                .url("https://reqres.in/api/users/2")
                .DELETE()
                .build();
        this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },

    downloadRequest() {
        var request = new okhttp.Request.Builder()
            .DOWNLOAD("https://archiveprogram.github.com/assets/img/direction/box2-home.png")
            .build();
        this.client.newCall(request).execute().then(this.onDownloadTaskStart).catch(this.onError);
    },

    multiDataRequest() {
        let requestBody1 = okhttp.RequestBody.create({Title: 'Multipart', Color: 'Brown'})
        let requestBody2 = okhttp.RequestBody.create("Okhttp")
        let requestBody3 = okhttp.RequestBody.create("MultiRequest")

        let multiPartObj = new okhttp.MultiPart.Builder()
                    .boundary('AaB03x')
                    .type(okhttp.MultiPart.FORMDATA)
                    .addPart(requestBody1)
                    .addPart(requestBody2)
                    .addPart(requestBody3)
                    .build();
        let body = multiPartObj.createRequestBody();
        var request =  new okhttp.Request.Builder()
            .url(this.echoServer)
            .POST(body)
            .addHeader("Content-Type", "multipart/form-data; boundary=AaB03x")
            .params("LibName", "Okhttp-ohos")
            .params("Request", "MultiData")
            .build();
        this.client.newCall(request).enqueue(this.onComplete, this.onError);
    },

    multipleRequests() {
        //GET
        let request1 = new okhttp.Request.Builder()
            .GET(this.echoServer)
            .addHeader("Content-Type", "application/json")
            .params("testKey1", "testValue1")
            .params("testKey2", "testValue2")
            .build();
        this.client.newCall(request1).enqueue(this.onComplete, this.onError);

        //POST
        let request2 = new okhttp.Request.Builder()
            .url(this.echoServer)
            .POST(okhttp.RequestBody.create("test123"))
            .addHeader("Content-Type", "application/json")
            .build();
        this.client.newCall(request2).enqueue(this.onComplete, this.onError);

        //POST with 2 values
        let request3 = new okhttp.Request.Builder()
            .url(this.echoServer)
            .POST(okhttp.RequestBody.create({
                a: 'a1', b: 'b1'
            }, new okhttp.Mime.Builder().contentType('application/json', 'charset', 'utf8').build().getMime()))
            .build();
        this.client.newCall(request3).enqueue(this.onComplete, this.onError);
        //POST with 3 values
        let formEncoder = new okhttp.FormEncoder.Builder()
            .add('key1', 'value1')
            .add('key2', 'value2')
            .add('key3', 'value3')
            .build();
        let fe_body = formEncoder.createRequestBody();

        var request4 = new okhttp.Request.Builder()
            .url(this.echoServer)
            .POST(fe_body)
            .build();
        this.client.newCall(request4).enqueue(this.onComplete, this.onError);

        //PUT request
        var request5 = new okhttp.Request.Builder()
            .url(this.echoServer)
            .PUT(okhttp.RequestBody.create({
                    a: 'a1', b: 'b1'
                }, new okhttp.Mime.Builder().contentType('application/json', 'charset', 'utf8').build().getMime()))
            .build();
        this.client.newCall(request5).enqueue(this.onComplete, this.onError);

        //DELETE request
        var request6 = new okhttp.Request.Builder()
            .url(this.echoServer)
            .DELETE()
            .build();
        this.client.newCall(request6).enqueue(this.onComplete, this.onError);
    },

    newCallRequest() {
        var request = new okhttp.Request.Builder()
            .url("https://postman-echo.com/post")
            .POST(okhttp.RequestBody.create("test123"))
            .addHeader("Content-Type", "application/json")
            .build();
        this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },

    setDefaultUserAgent(){
        var request = new okhttp.Request.Builder()
            .url("https://postman-echo.com/post")
            .POST(okhttp.RequestBody.create("test123"))
            .addHeader("Content-Type", "application/json")
            .setDefaultUserAgent()
            .build();
        this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },
    setDefaultContentType(){
        var request = new okhttp.Request.Builder()
            .url("https://postman-echo.com/post")
            .POST(okhttp.RequestBody.create("test123"))
            .addHeader("Content-Type", "application/json")
            .setDefaultContentType()
            .build();
        this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },
    setUserAgent(){
        var request = new okhttp.Request.Builder()
        .url("https://postman-echo.com/post")
        .POST(okhttp.RequestBody.create("test123"))
        .addHeader("Content-Type", "application/json")
        .setDefaultUserAgent("My Application 1.0")
        .build();
    this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },
    setContentType(){
        var request = new okhttp.Request.Builder()
        .url("https://postman-echo.com/post")
        .POST(okhttp.RequestBody.create("test123"))
        .setDefaultContentType("application/text")
        .build();
    this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },
    defaultConfigJSON(){
        var request = new okhttp.Request.Builder()
        .url("https://postman-echo.com/post")
        .POST(okhttp.RequestBody.create("test123"))
        .setDefaultConfig(defaultConfigJSON)
        .build();
    this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },

    setDebugModeFalse() {
        var request = new okhttp.Request.Builder()
            .url("https://postman-echo.com/post")
            .POST(okhttp.RequestBody.create("test123"))
            .addHeader("Content-Type", "application/json")
            .setdebugmode(false)
            .build();
        this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },
    setDebugModeTrue() {
        var request = new okhttp.Request.Builder()
            .url("https://postman-echo.com/post")
            .POST(okhttp.RequestBody.create("test123"))
            .addHeader("Content-Type", "application/json")
            .setdebugmode(true)
            .build();
        this.client.newCall(request).execute().then(this.onComplete).catch(this.onError);
    },

    //Encode normal string to gzip string
    gzipencodeTest() {

        const test = "hello, world!  adjasjdakjdakjdkjakjdakjskjasdkjaskjdajksdkjasdkjaksjdkja\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs";

        var compressed = okhttp.gZipUtil.gZipString(test);

        console.info("gzipencodeTest after gzip and compressed result is " + compressed);
    },

    //decode gzip string to normal string
    gzipdecodeTest() {

        const test = "hello, world!  adjasjdakjdakjdkjakjdakjskjasdkjaskjdajksdkjasdkjaksjdkja\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs";
        var compressed = okhttp.gZipUtil.gZipString(test);

        console.info("gzipencodeTest after gzip and compressed result is " + compressed);

        var restored = okhttp.gZipUtil.ungZipString(compressed);
        // Output to console
        console.info("gzipdecodeTest and uncompressed result is " + JSON.stringify(restored));
    },

    //Writes string to hello.txt and compresses to test.txt.gz
    async gzipEncodeFiletest() {

        var context = featureAbility.getContext();
        // @ts-ignore
        context.getCacheDir()
            .then((appInternalDir) => {

                console.info("gzipDecodeFiletest and  appInternalDir is " + appInternalDir);
                let fpath = appInternalDir + "/hello.txt";
                let fd = fileIO.openSync(fpath, 0o102, 0o666);
                // @ts-ignore
                fileIO.writeSync(fd, "hello, world!  adjasjdakjdakjdkjakjdakjskjasdkjaskjdajksdkjasdkjaksjdkja\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
                + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs",
                    function (err, bytesWritten) {
                });
                fileIO.closeSync(fd);

                //Till now string is written to hello.txt file. Now gzip hello.txt file to test.txt.gz
                let dest = appInternalDir + "/test.txt.gz";

                okhttp.gZipUtil.gZipFile(fpath, dest);
                console.info("gzipDecodeFile test success");
            })
            .catch((error) => {
                console.error('Failed to obtain the file directory. Cause: ' + error.message);
            })
    },

    //Working: decode from test.txt.gz file to hello2.txt file
    async gzipDecodeFiletest() {
        var context = featureAbility.getContext();
        // @ts-ignore
        context.getCacheDir()
            .then((appInternalDir) => {
                console.info("gzipDecodeFiletest and  appInternalDir is " + appInternalDir);

                let src = appInternalDir + "/test.txt.gz";
                let dest = appInternalDir + "/hello2.txt";

                okhttp.gZipUtil.ungZipFile(src, dest);
                console.info("gzipDecodeFiletest success");
            })
            .catch((error) => {
                console.error('Failed to obtain the file directory. Cause: ' + error.message);
            })
    },

    gzipreceive() {
        var reqBuilder = new okhttp.RequestBuilder();
        reqBuilder
            .GET('http://121.244.27.58:12272/zip/downloadzipfile/Test2.zip')
            .header("Content-Type", "application/json")
            .connectTimeout(10000)
            .buildAndExecute()
            .then(this.onComplete)
            .catch(this.onError);

        reqBuilder.interceptors.response.use(resp => {
            //Temp begin for testing
            // Print response data and request URL.
            console.log("URL - " +resp.url);
            console.log("Response Data"+ resp.result )
            //temp end
            return resp;
        });
    },

    gzipreceive2() {
        var reqBuilder = new okhttp.RequestBuilder();
        reqBuilder
            .GET('http://121.244.27.58:12269/zip/downloadzipfile/Test2.zip')
            .header("Content-Type", "application/json")
            .connectTimeout(10000)
            .buildAndExecute()
            .then(this.onComplete)
            .catch(this.onError);

        reqBuilder.interceptors.response.use(resp => {
            //Temp begin for testing
            // Print response data and request URL.
            console.log("URL - " +resp.url);
            console.log("Response Data"+ resp.result )
            //temp end
            return resp;
        });
    },

    onGzipRequest: function (req) {
        console.log("onGzipRequest");

        req.addHeader("Content-Encoding", "gzip");
        console.log("onGzipRequest onComplete body : "+req.body.content);
        console.log("onGzipRequest onComplete header  : "+JSON.stringify(req.headers));
        var compressed = okhttp.gZipUtil.gZipString(req.body.content);
        req.body.content = compressed;
        console.log("onGzipRequest compressed is : " +compressed);
        return req;
    },

    onGzipResponse: function (res) {
        console.log("onGzipResponse");

        return res;
    },

    postGzipText() {

        const teststr = "hello, world!  adjasjdakjdakjdkjakjdakjskjasdkjaskjdajksdkjasdkjaksjdkja\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs\n"
        + "adasajsdkjadjkakjdakjsdkjadkjakjdakjsdkjasdkjaskjdakjsdkjaskjdakjsdkjaskjakjdakjs";

        var request = new okhttp.Request.Builder()
            .url("http://121.244.27.58:12272/postgziptext")
            .POST(okhttp.RequestBody.create(teststr))
            .addHeader("Content-Type", "application/json")
            .addInterceptor(this.onGzipRequest, this.onGzipResponse)
            .build();

        this.client.newCall(request).enqueue(this.onComplete, this.onError);
    },

    onGzipRequest2: function (req) {
        console.log("onGzipRequest2");
        req.addHeader("Content-Encoding", "gzip");
        return req;
    },

    onGzipResponse2: function (res) {

        this.content = JSON.parse(res.result);

        console.log("onGzipResponse2 its a gzip header & content "+ this.content);
        var restored = okhttp.gZipUtil.ungZipString(this.content);
        console.log("onGzipResponse and uncompressed result is " + restored);

        return restored;
    },

    GetGzipText() {
        var request = new okhttp.Request.Builder()
            .GET("http://121.244.27.58:12272/getgziptext")
            .addHeader("Content-Type", "application/json")
            .addInterceptor(this.onGzipRequest2, this.onGzipResponse2)
            .build();

        this.client.newCall(request).enqueue(this.onComplete, this.onError);
    },

}